<template>
<div>
  <el-dialog
    title="定时开关机设置"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading"
             label-width="120px">
      <el-row :gutter="15">

        <el-col :span="20">
            <el-form-item label="启用定时" prop="powerOnTime"
                :rules="[
                 ]">
               <el-switch
                 v-model="powerSwitch">
               </el-switch>
            </el-form-item>
            <el-form-item label="开机时间" prop="powerOnTime"
                :rules="powerSwitch ? [
                   {required: true, message:'请选择开机时间', trigger:'blur'}
                 ] : []">
                <el-time-select
                    placeholder="开机时间"
                    v-model="inputForm.powerOnTime"
                    :picker-options="{
                      start: '00:00',
                      step: '00:05',
                      end: '24:00'
                    }" :disabled="!powerSwitch">
                  </el-time-select>
           </el-form-item>
           <el-form-item label="关机时间" prop="powerOffTime"
                :rules="powerSwitch ? [
                   {required: true, message:'请选择关机', trigger:'blur'}
                 ] : []">
                <el-time-select
                  placeholder="关机时间"
                  v-model="inputForm.powerOffTime"
                  :picker-options="{
                    start: '00:00',
                    step: '00:05',
                    end: '24:00',
                    minTime: inputForm.powerOnTime
                  }" :disabled="!powerSwitch">
                </el-time-select>
           </el-form-item>
           <div style="padding-left: 52px;">定时开关机设置后，两分钟内生效，可刷新列表数据查看</div>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        powerSwitch: true,
        visible: false,
        loading: false,
        inputForm: {
          devId: '',
          powerOnTime: '',
          powerOffTime: ''
        }
      }
    },
    components: {
    },
    methods: {
      init (method, ids) {
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.devId = ids
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            var param = {
              powerOnTime: this.inputForm.powerOnTime,
              powerOffTime: this.inputForm.powerOffTime
            }
            if (!this.powerSwitch) {
              param.powerOnTime = '00:00'
              param.powerOffTime = '00:00'
            }
            var p = {
              devId: this.inputForm.devId,
              type: '2',
              param: JSON.stringify(param),
              state: -1,
              progress: 0,
              result: ''
            }
            this.$http({
              url: `/cmd/jfCmd/save`,
              method: 'post',
              data: p
            }).then(({data}) => {
              this.$message.success(data.msg)
              if (data && data.success) {
                this.visible = false
                // this.$emit('issueTaskStart', data.list)
                // 下发成功后，把数据丢给父页面进行定时查看下载进度
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
